﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Key_2 : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
    public bool isAuto = true;
    AudioClip clip;

    AudioClip[] clips = new AudioClip[7];
    Image image;
    Transform effect;
    private void Start()
    {
        image = this.GetComponent<Image>();
        effect = Manage_2.instance.canvas.transform.Find("Effect");
    }

    void PlaySound()
    {
        if(isAuto)
        {
            AudioSource.PlayClipAtPoint(clips[Manage_2.instance.currentMusic], Camera.main.transform.position);
        }
        else
        {
            AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
        }
    }
    void Vibrate()
    {
        GameFunction.Vibrate(GameData.keyVibrateIndex);
    }
    void GenerateMusicNote()
    {
        GameObject musicNote = Manage_2.instance.musicalNote_prefab;
        //仅适用于当前Canvas布局
        Vector3 pos = this.transform.position;
        pos.z = 0;
        GameObject.Instantiate(musicNote, pos, Quaternion.identity, effect);
    }

    public void SetClip(AudioClip[] clips)
    {
        this.clips = clips;
    }
    public void SetClip(AudioClip clip)
    {
        this.clip = clip;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        image.color = new Color(0.7843137f, 0.7843137f, 0.7843137f, 1);
        PlaySound();
        GenerateMusicNote();
        Vibrate();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        image.color = new Color(1, 1, 1, 1);
    }
}